﻿using System.Threading;
using System.Windows;
using System.Windows.Input;
using Flurl.Http;
using HandyControl.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SoonCode.Core;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Models;


namespace SoonCode.FocusTest.ViewModels
{
    class SettingViewModel : BindableBase, IDialogAware
    {
        private string orgName = "";
        private string printFoot = "";
        private string _testNumber = "无限制";
        private string _courseNumber = "无限制";
        private string _expireDate = "无限制";
        public ICommand CloseDialogCommand { get; set; }
        public ICommand SaveDataCommand { get; set; }
        public String Title { get; set; } = "系统设置";
        private Visibility _serviceFrequency = Visibility.Hidden;
        private Visibility _serviceTime=Visibility.Hidden;

        public string ExpireDate
        {
            get { return _expireDate; }
            set
            {
                _expireDate=value;
                RaisePropertyChanged();
            }
        }
        public Visibility ServiceTime
        {
            get { return _serviceTime; }
            set
            {
                _serviceTime=value;
                RaisePropertyChanged();
            }
        }

        public Visibility ServiceFrequency
        {
            get { return _serviceFrequency; }
            set
            {
                _serviceFrequency = value;
                RaisePropertyChanged();
            }
        }
        public string OrgName
        {
            get { return orgName; }
            set
            {
                orgName = value;
                RaisePropertyChanged();
            }
        }

        public string PrintFoot
        {
            get
            {
                return printFoot;
            }
            set
            {
                printFoot=value;
                RaisePropertyChanged();
            }
        }

        public string TestNumber
        {
            get { return _testNumber; }
            set
            {
                _testNumber=value;
                RaisePropertyChanged();
            }
        }
        public string CourseNumber
        {
            get
            {
                return _courseNumber;
            }
            set
            {
                _courseNumber=value;
                RaisePropertyChanged();
            }
        }

        public SettingViewModel(IEventAggregator aggregator)
        {
            CloseDialogCommand = new DelegateCommand(() =>
            {
                RequestClose.Invoke(null);
            });
            SaveDataCommand = new DelegateCommand(() => { SaveData(); });
        }

        public event Action<IDialogResult> RequestClose;


        private async void SaveData()
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict["c_name"] = OrgName;
            dict["c_printFoot"] = PrintFoot;
            bool res = await API.SaveOrgCustomInfo(dict);
            if (res)
            {
                GlobalModel.Instance.OrgInfo = await API.GetCurrOrg();
                Growl.SuccessGlobal("保存成功");
            }
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            OrgInfo info = GlobalModel.Instance.OrgInfo;
            this.OrgName = info.c_name;
            this.PrintFoot = info.c_printFoot;
            if (info.c_name.IsNullOrEmpty())
            {
                this.OrgName = info.name;
            }

            if (info.serviceType=="frequency")
            {
                ServiceFrequency = Visibility.Visible;
                TestNumber = info.testNumber+"";
                CourseNumber = info.courseNumber + "";
            }

            if (info.serviceType == "time"||info.serviceType.IsNullOrEmpty())
            {
                ServiceTime = Visibility.Visible;
                if (!info.expireDate.IsNullOrEmpty())
                {
                    string[] dateSplit = info.expireDate.Split("-");
                    ExpireDate = $"{dateSplit[0]}年{dateSplit[1]}月{dateSplit[2]}日";
                }

            }
        }
    }
}
